﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Threading;
using System.Windows.Threading;

namespace ParticleFiltersDemo1
{
    public partial class MainPage : UserControl
    {
        Controller controller;
        DispatcherTimer timer;

        Canvas layoutRoot;
        int stepSize = 50;

        public MainPage()
        {
            InitializeComponent();
            
            layoutRoot = FindName( "LayoutRoot" ) as Canvas;
            this.controller = new Controller();
            this.timer = new DispatcherTimer();
            this.timer.Interval = TimeSpan.FromSeconds( 1 );
            this.timer.Tick += new EventHandler( timer_Tick );

            this.DataContext = this.controller;


            drawLines();
            drawOpsticles();
            this.timer.Start();
        }

        private void timer_Tick( object sender, EventArgs e )
        {
            this.controller.Robot.Move();
         /*   int count = this.controller.Particles.Count;
            for (int i = 0; i < count; i++)
            {
                this.controller.Particles.particles[i].AdvanceBy(1,false);
            }*/
        }

        void drawLines()
        {
            SolidColorBrush stroke = new SolidColorBrush();

            Board board = new Board();
            Point previousPoint = new Point( 10, 10 );
            Point newPoint = new Point( board.sizeX * stepSize, 10 );
            for ( int i = 0; i < board.sizeX + 1; i++ )
            {
                Line line = new Line();
                line.X1 = previousPoint.X;
                line.Y1 = previousPoint.Y;
                line.X2 = newPoint.X;
                line.Y2 = newPoint.Y;
                previousPoint.Y += stepSize;
                newPoint.Y += stepSize;
                line.StrokeThickness = 1;
                stroke.Color = Colors.Black;
                line.Stroke = stroke;
                line.SetValue( Canvas.ZIndexProperty, ( object )( -1 ) );

                layoutRoot.Children.Add( line );
            }
            previousPoint = new Point( 10, 10 );
            newPoint = new Point( 10, board.sizeY * stepSize );
            for ( int i = 0; i < board.sizeY + 1; i++ )
            {
                Line line = new Line();
                line.X1 = previousPoint.X;
                line.Y1 = previousPoint.Y;
                line.X2 = newPoint.X;
                line.Y2 = newPoint.Y;
                previousPoint.X += stepSize;
                newPoint.X += stepSize;
                line.StrokeThickness = 1;
                stroke.Color = Colors.Black;
                line.Stroke = stroke;
                line.SetValue( Canvas.ZIndexProperty, ( object )( -1 ) );

                layoutRoot.Children.Add( line );
            }            
        }

        void drawOpsticles()
        {
            Board board = new Board();
            for ( int i = 0; i < board.sizeX; i++ )
            {
                for ( int j = 0; j < board.sizeY; j++ )
                {
                    if ( board.board[i, j] == 1 )
                    {
                        drawSquare( i * stepSize + 10, j * stepSize + 10 );
                    }
                }
            }
        }
        void drawSquare( int x, int y )
        {

            Rectangle rect = new Rectangle();
            Point origPoint = new Point( x, y );
            //   origPoint = e.GetPosition(draw);
            Canvas.SetLeft( rect, origPoint.X );
            Canvas.SetTop( rect, origPoint.Y );
            rect.Stroke = new SolidColorBrush( Colors.Purple );
            rect.Fill = new SolidColorBrush( Colors.Black );
            // rect.StrokeThickness = 2;
            rect.Height = stepSize;
            rect.Width = stepSize;
            rect.SetValue( Canvas.ZIndexProperty, ( object )( -1 ) );

            layoutRoot.Children.Add( rect );
        }

        void drawRobbot( double x, double y )
        {
            //layoutRoot.Children.Remove( robbie );
            //layoutRoot.Children.Remove( sensor );

            //Point origPoint = new Point( x, y );
            //Canvas.SetLeft( robbie, origPoint.X - stepSize / 2 );
            //Canvas.SetTop( robbie, origPoint.Y - stepSize / 2 );
            //robbie.Stroke = new SolidColorBrush( Colors.Purple );
            //robbie.Fill = new SolidColorBrush( Colors.Purple );
            //robbie.Height = stepSize / 2;
            //robbie.Width = stepSize / 2;

            //Canvas.SetLeft( sensor, origPoint.X + robbie.Height / 2 - stepSize / 2 );
            //Canvas.SetTop( sensor, origPoint.Y + robbie.Width / 2 - stepSize / 2 );
            //sensor.Fill = new SolidColorBrush( Colors.Yellow );
            //sensor.Width = robbie.Width / 3;
            //sensor.Height = robbie.Height / 3;

            //layoutRoot.Children.Add( robbie );
            //layoutRoot.Children.Add( sensor );
        }

        void doTheMagic()
        {
            ParticlesSet particles = this.controller.Particles;
            
            bool canGoFurther = true;
            DateTime dt = DateTime.Now;

            // while ( canGoFurther == true )
            {
                canGoFurther = this.controller.Robot.Move();

                while ( DateTime.Now.Subtract( dt ).Seconds < 30 )
                    ;

                dt = DateTime.Now;
                canGoFurther = this.controller.Robot.Move();
            }
        }

        private void btn_Click( object sender, RoutedEventArgs e )
        {
            this.timer.Start();
        }
    }


}